//
//  LocationViewController.m
//  BaChat
//
//  Created by dllo on 16/6/25.
//  Copyright © 2016年 刘洋. All rights reserved.
//

#import "LocationViewController.h"
#import <MAMapKit/MAMapKit.h>
#import <AMapFoundationKit/AMapFoundationKit.h>
#import <AMapSearchKit/AMapSearchKit.h>
#define APIKey @"19f050897241dbae6bcec94adab45f93"
#define ScreenWidth [[UIScreen mainScreen] bounds].size.width
#define ScreenHeight [[UIScreen mainScreen] bounds].size.height
@interface LocationViewController ()<MAMapViewDelegate,AMapSearchDelegate>
@property(nonatomic,strong)MAMapView *mapView;//地图对象属性
@property(nonatomic,strong)CLLocation *currentlocation;//当前位置
@property(nonatomic,strong)AMapSearchAPI *search;//搜索对象属性
@property (nonatomic, strong)NSMutableDictionary *tempDic;

@end

@implementation LocationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.navigationItem.title = @"实时位置";
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[[UIImage imageNamed:@"navigationbar_back_light"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(leftAction)];
    
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"发送" style:UIBarButtonItemStylePlain target:self action:@selector(sendAction)];
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    //    self.tempArr = [NSMutableArray array];
    self.tempDic = [NSMutableDictionary dictionary];
    
    [self createMapView];
    
}


-(void)leftAction{
    [self.navigationController popViewControllerAnimated:YES];
}




-(void)createMapView{
    //设置APIKey
    
    [AMapServices sharedServices].apiKey = APIKey;
    
    
    self.mapView = [[MAMapView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
    
    self.mapView.delegate = self;
    //罗盘位置
    self.mapView.compassOrigin = CGPointMake(self.mapView.compassOrigin.x, 22);
    //比例尺位置
    self.mapView.scaleOrigin = CGPointMake(self.mapView.scaleOrigin.x, self.mapView.compassOrigin.y);
    [self.view addSubview:self.mapView];
    
    //打开用户定位
    self.mapView.showsUserLocation = YES;
    
    //设置跟踪模式
    [self.mapView setUserTrackingMode:MAUserTrackingModeFollow animated:YES];
    
}
//当位置更新时，会进定位回调，通过回调函数，能获取到定位点的经纬度坐标
-(void)mapView:(MAMapView *)mapView didUpdateUserLocation:(MAUserLocation *)userLocation updatingLocation:(BOOL)updatingLocation{
    if (updatingLocation) {
        NSLog(@"====latitude : %f,----longitude: %f,",userLocation.coordinate.latitude,userLocation.coordinate.longitude);
        
        [self.tempDic setObject:[NSString stringWithFormat:@"%f",userLocation.coordinate.latitude] forKey:@"latitude"];
        [self.tempDic setObject:[NSString stringWithFormat:@"%f",userLocation.coordinate.longitude] forKey:@"longitude"];
        
        NSLog(@"232435456545=====%@",self.tempDic);
        
        self.currentlocation = [userLocation.location copy];
        
    }
}

//在选中用户位置annotation时弹出当前地址
-(void)mapView:(MAMapView *)mapView didSelectAnnotationView:(MAAnnotationView *)view{
    // 选中定位annotation的时候进行逆地理编码查询
    if ([view.annotation isKindOfClass:[MAUserLocation class]]) {
        
        [self reGeoAction];
    }
    
    
}

//逆地理编码 发起搜索请求
-(void)reGeoAction{
    if (self.currentlocation) {
        AMapReGeocodeSearchRequest *request = [[AMapReGeocodeSearchRequest alloc] init];
        request.location = [AMapGeoPoint locationWithLatitude:self.currentlocation.coordinate.latitude longitude:self.currentlocation.coordinate.longitude];
        [self.search AMapReGoecodeSearch:request];
    }
    
}


-(void)sendAction{
    
    
    
    //latitude纬度 longitude
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"sendAddress" object:self.tempDic];
    
    [self.navigationController popViewControllerAnimated:YES];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
